#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
using namespace std;
using ull = unsigned long long;
using i128 = __int128_t;
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
#define int long long

const int N = 2e5+10;

int e[N],ne[N],idx,h[N];
int e1[N],ne1[N],idx1,h1[N];

int n,m;

void add(int a,int b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}
void add1(int a,int b)
{
    e1[idx1] = b;
    ne1[idx1] = h1[a];
    h1[a] = idx1++;
}
int arr[N];
int brr[N];
int ans = 1;

void dfs(int x,int y,int cnt)
{
    if(arr[x] != brr[y]) return;
    ans = max(ans,cnt + 1);
    for(int i = h[x]; i != -1; i = ne[i])
    {
        for(int i1 = h1[y]; i1 != -1; i1 = ne1[i1])
        {
            dfs(e[i],e1[i1],cnt + 1);
        }
    }
}

void solve()
{  
    memset(h,-1,sizeof h);
    memset(h1,-1,sizeof h1);
    cin >> n >> m;
    for(int i = 1; i <= n; i ++) cin >> arr[i];
    for(int i = 1; i <= m; i ++) cin >> brr[i];
    for(int i = 1; i <= n - 1; i ++)
    {
        int a,b;
        cin >> a >> b;
        if(a > b) swap(a,b);
        add(a,b);
    }
    for(int i = 1; i <= m - 1; i ++)
    {
        int a,b;
        cin >> a >> b;
        if(a > b) swap(a,b);
        add1(a,b);
    }
    dfs(1,1,0);
    cout << ans << endl;
}
signed main()
{
    int t = 1;
    //cin >> t;
    while(t --)
    {
        solve();
    }
}
